//
//  MSFilterTableViewController.m
//  JYG_Agent
//
//  Created by apple on 15/10/19.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MSFilterTableViewController.h"

@interface MSFilterTableViewController ()
@property NSIndexPath * curIndexPath;
@property (nonatomic,strong)MSSubFilterTableViewController* subListVctr;
@end

@implementation MSFilterTableViewController
@synthesize dataArray,curIndexPath;

-(void)setDataArray:(NSArray*)array
{
    dataArray=[[NSMutableArray alloc] initWithArray:array];
    curIndexPath=nil;
    self.tableView.contentOffset=CGPointZero;
    self.subListVctr.tableView.contentOffset=CGPointZero;
    
    [self.tableView reloadData];
    [self.subListVctr.tableView reloadData];
    [self scrollToSelectedIndex];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.bounces=NO;
    self.tableView.showsVerticalScrollIndicator=NO;
    if (!self.showSeperatorLine) {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
}

- (void)dismissSubViews
{
    self.curIndexPath = nil;
    [self.subListVctr dismissSubViews];
}

-(void)scrollToSelectedIndex
{
    int index=0;
    for (int i=0;i<self.dataArray.count;i++)
    {
        NSObject* item=[self.dataArray objectAtIndex:i];
        if ([item isKindOfClass:[NSString class]])
        {
            if ([(NSString*)item isEqualToString:(NSString*)self.selected])
            {
                index=i;
            }
        }
        else if ([item isKindOfClass:[NSDictionary class]])
        {
            NSDictionary* dic=(NSDictionary*)item;
            NSString* key=[[dic allKeys] firstObject];
            NSArray* subItems=[dic objectForKey:key];
            if (([key isEqualToString:(NSString*)self.selected]||[subItems containsObject:self.selected]))
            {
                index=i;
            }
        }
    }
    
    [self.tableView scrollRectToVisible:CGRectMake(0, self.cellHeight*index, APPFRAME.size.width, 280) animated:NO];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* indentifer=@"indentiferCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.tintColor=kBlueColor;
        cell.textLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        if (self.showSeperatorLine) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(15, self.cellHeight, APPFRAME.size.width-30, 0.5)];
            line.image = [UIImage imageNamed:@"dot"];
            [cell.contentView addSubview:line];
        }
    }
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.textLabel.textColor=kDarkGrayColor;
    cell.backgroundColor=[UIColor clearColor];
    
    NSObject* obj=[self.dataArray objectAtIndex:indexPath.row];
    if ([obj isKindOfClass:[NSString class]])
    {
        [_subListVctr.view removeFromSuperview];
        cell.textLabel.text=[self.dataArray objectAtIndex:indexPath.row];
        if ([cell.textLabel.text isEqualToString:(NSString*)self.selected])
        {
            cell.textLabel.textColor=kBlueColor;
        }
    }
    else if ([obj isKindOfClass:[NSDictionary class]])
    {
        NSDictionary* item=(NSDictionary*)obj;
        NSString* key=[[item allKeys] firstObject];
        NSArray* subItems=[item objectForKey:key];
        if ((indexPath.section==curIndexPath.section&&indexPath.row==curIndexPath.row)
            ||(self.selected!=nil&&([key isEqualToString:(NSString*)self.selected]||[subItems containsObject:self.selected])&&curIndexPath!=nil))
        {
            if (!self.subListVctr.view.superview) {
                [self.view.superview addSubview:self.subListVctr.view];
            }
            cell.backgroundColor=[UIColor whiteColor];
            self.subListVctr.selected=self.selected;
            self.subListVctr.selectedKey=key;
            if ([key isEqualToString:(NSString*)self.selected]) {
                self.subListVctr.selected = @"不限";
            }
            self.subListVctr.dataArray=(NSMutableArray*)subItems;
            self.subListVctr.view.frame=CGRectMake(APPFRAME.size.width/3.0,0, APPFRAME.size.width*2/3.0, self.view.height);
            self.subListVctr.view.hidden=NO;
            self.selectedKey=key;
            curIndexPath=indexPath;
        }
        
        cell.textLabel.text=key;
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectedData:(NSString *)data{}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self handleSelectedRowAtIndexPath:indexPath forTableView:tableView];
}

- (void)handleSelectedRowAtIndexPath:(NSIndexPath *)indexPath forTableView:(UITableView *)tableView
{
    if (tableView==self.tableView)
    {
        _subListVctr.curIndexPath = nil;
        [_subListVctr.thirdLayerFilter.view removeFromSuperview];
        NSObject* obj=[self.dataArray objectAtIndex:indexPath.row];
        if ([obj isKindOfClass:[NSDictionary class]])
        {
            NSDictionary* item=(NSDictionary*)obj;
            NSString* key=[[item allKeys] firstObject];
            self.selected = [self.selectedDic objectForKey:key];
            curIndexPath=indexPath;
            [tableView reloadData];
            return;
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSObject* data;
    if (tableView!=self.tableView)
    {
        self.selected=self.subListVctr.selected;
        data=@{self.selectedKey:self.selected};
        [self.tableView reloadData];
    }
    else
    {
        self.selected=[self.dataArray objectAtIndex:indexPath.row];
        data=self.selected;
    }
    
    if ([(NSString*)self.selected isEqualToString:@"不限"])
    {
        self.selected=self.selectedKey;
    }
    
    if([self.delegate respondsToSelector:@selector(tableView: didSelectedData:)])
    {
        [self.delegate performSelector:@selector(tableView: didSelectedData:) withObject:tableView withObject:data];
    }
}

- (void)setDefaultSelected:(NSIndexPath *)defaultSelected
{
    _defaultSelected = defaultSelected;
    [self handleSelectedRowAtIndexPath:defaultSelected forTableView:self.tableView];
}

-(MSSubFilterTableViewController*)subListVctr
{
    if (_subListVctr==nil)
    {
        _subListVctr=[[MSSubFilterTableViewController alloc]initWithStyle:UITableViewStylePlain];
        _subListVctr.delegate=(id)self;
        _subListVctr.view.backgroundColor=[UIColor whiteColor];
        _subListVctr.view.frame=CGRectMake(APPFRAME.size.width/3.0, 0, APPFRAME.size.width*2/3.0, self.view.height);
        _subListVctr.view.hidden=YES;
        _subListVctr.enableRefresh=NO;
        _subListVctr.cellHeight=40.0;
        _subListVctr.tableView.bounces=NO;
    }
    return _subListVctr;
}

@end
